package spacegame.main;
import org.andengine.engine.camera.ZoomCamera;
import org.andengine.extension.physics.box2d.util.Vector2Pool;
import com.badlogic.gdx.math.Vector2;


/**
*
* @author Muiota Betarho
* @since 20:00:00 - 20.10.2012
*/
public class SpaceCamera extends ZoomCamera {

	// ===========================================================
	// Fields
	// ===========================================================
	
	private float m_currentX;
	private float m_targetCenterY;
	private float m_targetCenterX;
	private float m_currentY;
	private float m_currentZoomFactor ;
	private float m_targetZoomFactor ;
	private float m_currentDifferentY;
	private float m_currentDifferentX;
	private float m_targetDifferentX;
	private float m_targetDifferentY;
	private float m_cameraSpeed = 2f;
	private float m_cameraInertial = 1.2f;
	private float m_inverseWidth;
	private float m_inverseCameraSpeed;
	
	// ===========================================================
	// Constructors
	// ===========================================================

	public SpaceCamera(float pX, float pY, float pWidth, float pHeight) {
		super(pX, pY, pWidth, pHeight);
		m_currentX = this.getCenterX();
		m_currentY = this.getCenterY();
		m_targetCenterX = this.getCenterX();
		m_targetCenterX = this.getCenterY();
		m_currentZoomFactor = this.mZoomFactor;
		m_targetZoomFactor = this.mZoomFactor;
		m_currentDifferentX = 0;
		m_currentDifferentY = 0;
		m_inverseWidth = 4 / getWidth();
		m_inverseCameraSpeed =  1 / m_cameraSpeed;
	}
	


    //Draw camera frame
	//pSecondsElapsed - frame time
	@Override
	public void onUpdate(final float pSecondsElapsed) {
		
		final float _differentX = m_targetCenterX - m_currentX;
		final float _differentY = m_targetCenterY - m_currentY;

		final float _secondsElapsed =pSecondsElapsed>m_inverseCameraSpeed ? m_inverseCameraSpeed : pSecondsElapsed;
		m_currentX = m_currentX + _differentX * _secondsElapsed * m_cameraSpeed;
		m_currentY = m_currentY + _differentY * _secondsElapsed * m_cameraSpeed;

		final float _differentDifferentX = (m_targetDifferentX - m_currentDifferentX);
		final float _differentDifferentY = (m_targetDifferentY - m_currentDifferentY);
		m_currentDifferentX = m_currentDifferentX + _differentDifferentX
				* _secondsElapsed * m_cameraSpeed;
		m_currentDifferentY = m_currentDifferentY + _differentDifferentY
				* _secondsElapsed * m_cameraSpeed;

		final Vector2 _differentVector = Vector2Pool.obtain(_differentX, _differentY);
		final float _zoom = _differentVector.len();
		final int m_peaceZoom = 2;
		if (_zoom < 0.2f) {
			m_targetZoomFactor = m_peaceZoom;
		} else {
			m_targetZoomFactor = m_peaceZoom - _zoom * m_inverseWidth ;
			if (m_targetZoomFactor<0.5f) m_targetZoomFactor = 0.5f;
		}
		final float m_differentZoom = (m_targetZoomFactor - m_currentZoomFactor);
		m_currentZoomFactor = m_currentZoomFactor + m_differentZoom
				* _secondsElapsed;
		super.setZoomFactor(m_currentZoomFactor);
		super.onUpdate(_secondsElapsed);
	}
    
	// Forces the camera to zoom
	@Override
	public void setZoomFactor(final float pZoomFactor) {
	
		super.setZoomFactor(m_currentZoomFactor);
	}
	
	
	// Forces the camera to move in fixed steps
	@Override
	public void setCenter(float pCenterX, float pCenterY) {

		m_targetDifferentX = (m_targetCenterX - m_currentX) * m_cameraInertial;
		m_targetDifferentY = (m_targetCenterY - m_currentY) * m_cameraInertial;

		super.setCenter(m_currentX, m_currentY);

		m_targetCenterX = pCenterX + m_currentDifferentX;
		m_targetCenterY = pCenterY + m_currentDifferentY;

	}
      
}